package org.ucas.faker.web.entity.patents;

import org.ucas.faker.web.utils.model.RawPatent;

import java.lang.reflect.Field;
import java.util.Set;

public interface Patentable {

  default <T> void of(T patent, boolean overwrite) {
    Field[] fields = this.getClass().getDeclaredFields();
    Set<String> ex = exclude();
    for(Field field:fields) {
      try {
        if(ex != null && ex.contains(field.getName())) continue;
        Field src = patent.getClass().getDeclaredField(field.getName());


        src.setAccessible(true);
        field.setAccessible(true);

        if(!overwrite && field.get(this) != null) continue;

        field.set(this,src.get(patent));
      } catch (NoSuchFieldException e) {
        // do nothing
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }

    }
  }

  default <T> void to(T patent, boolean overwrite) {
    Field[] fields = this.getClass().getDeclaredFields();
    Set<String> ex = exclude();
    for(Field field:fields) {
      try {
        if(ex != null && ex.contains(field.getName())) continue;
        Field dst = patent.getClass().getDeclaredField(field.getName());


        dst.setAccessible(true);
        field.setAccessible(true);

        if(!overwrite && dst.get(patent) != null) continue;

        dst.set(patent,field.get(this));
      } catch (NoSuchFieldException e) {
        // do nothing
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }

    }
  }

  default Set<String> exclude() {
    return null;
  }
}
